package com.test.clyq.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.cplatform.util2.TimeStamp;
import com.test.clyq.entity.VisitLogsCountResult;
import com.test.clyq.form.VisitLogType;
import com.test.clyq.service.VisitLogsService;
import com.test.json.JsonUtil;
import com.test.util.FormatExceptionStack;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * VisitLogsAction. <br>
 * 用户访问日志管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class VisitLogsAction extends BaseAction {

	public static String getTime(int day) {
		// Format f = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, day);
		return new SimpleDateFormat("yyyyMMdd").format(c.getTime());
	}

	public ActionForward getStatsType(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		try {
			List<VisitLogType> statsTypeList = new ArrayList<VisitLogType>();

			VisitLogType one = new VisitLogType();
			one.setId(1);
			one.setName("phone");
			one.setDesc("手机用户数");
			statsTypeList.add(one);

			VisitLogType two = new VisitLogType();
			two.setId(1);
			two.setName("title");
			two.setDesc("内容访问量");
			statsTypeList.add(two);

			String jsonStr = JsonUtil.getJsonStr(statsTypeList, null, -1);
			this.log.debug("获得用户列表成功");
			this.log.info("获得用户列表end");
			print(response, jsonStr, null, null);
		}
		catch (Exception e) {
			this.log.error("获得统计对象:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 获得用户访问日志列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getVisitLogs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得用户访问日志列表start");
		VisitLogsService visitLogsService = (VisitLogsService) getBean("visitLogsService");
		try {
			String searcyType = getParameter(request, "searcyType");
			String name = getParameter(request, "name");
			String tmp_startTime = getParameter(request, "startTime");
			String tmp_endTime = getParameter(request, "endTime");

			if (StringUtils.isEmpty(tmp_startTime)) {
				tmp_startTime = getTime(-7);
			} else {
				tmp_startTime = TimeStamp.FormatTime(tmp_startTime, "yyyy-MM-dd", "yyyyMMdd");
			}
			if (StringUtils.isEmpty(tmp_endTime)) {
				tmp_endTime = TimeStamp.getTime(TimeStamp.YYYYMMDD);
			} else {
				tmp_endTime = TimeStamp.FormatTime(tmp_endTime, "yyyy-MM-dd", "yyyyMMdd");
			}
			if (StringUtils.isEmpty(searcyType)) {
				searcyType = "phone";
			}
			tmp_startTime = tmp_startTime + "000000";
			tmp_endTime = tmp_endTime + "000000";

			System.out.println(searcyType + " == " + name);
			List<VisitLogsCountResult> list = visitLogsService.getVisitLogByTime(tmp_startTime, tmp_endTime, searcyType, name);

			List<VisitLogsCountResult> listAll = new ArrayList<VisitLogsCountResult>();
			long num_all = 0;
			if (list != null && !list.isEmpty()) {
				for (VisitLogsCountResult vlcr : list) {
					num_all = num_all + vlcr.getCount();
					listAll.add(vlcr);
				}
			}
			if (!(name != null && !"".equals(name))) {
				VisitLogsCountResult vlcr = new VisitLogsCountResult();
				vlcr.setCategoryName("总访问量");
				vlcr.setCode(searcyType + "_all");
				vlcr.setCount(num_all);
				listAll.add(vlcr);
			}
			String jsonStr = JsonUtil.getJsonStringFromCollection(listAll, new String[] { "categoryName", "count", "code" });

			String str = "{totalCount:50,results:[";

			str += jsonStr;
			str += "]}";
			this.log.debug("获得用户访问日志列表成功");
			this.log.info("获得用户访问日志列表end");
			print(response, str, null, null);
		}
		catch (Exception e) {
			this.log.error("获得用户访问日志:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

}
